package main

import (
	"database/sql"
	"fmt"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	driverName := "mysql"
	dsn := "golang:zxc,./123@tcp(43.139.115.142:3306)/user?charset=utf8mb4&loc=Local&parseTime=true"
	db, err := sql.Open(driverName, dsn)
	if err != nil {

		fmt.Println(err)
		return
	}
	defer db.Close()
	if err = db.Ping(); err != nil {
		fmt.Println(err)
		return
	}

	sql := `
	insert into users(name,password,birthday) values ('sonfer','xxxx','1997-01-20');
	`
	result, err := db.Exec(sql)
	if err != nil {
		fmt.Println(err)
		return
	}

	// 最后插入id
	fmt.Println(result.LastInsertId())
	// 影响行数
	fmt.Println(result.RowsAffected())

	// 更新
	sql = `
	update users 
	set birthday=now();
	`
	result, err = db.Exec(sql)
	fmt.Println(result.LastInsertId())
	fmt.Println(result.RowsAffected())

	// 删除
	sql = `
	delete from users where name='sonfer';
	`
	result, err = db.Exec(sql)
	fmt.Println(result.LastInsertId())
	fmt.Println(result.RowsAffected())

	sql = `
	create table user1 (
		id bigint,
		name varchar(32),
		password varchar(1024),
		sex boolean,
		birthday datetime,
		addr text,
		tel varchar(32),
		index idx_name(name)
	) engine=innodb default charset utf8mb4 comment="";
	`
	result, _ = db.Exec(sql)
	fmt.Println(result.LastInsertId())
	fmt.Println(result.RowsAffected())
}
